import type { MenuVO } from '@/server/menu/types';
import { type DirectiveBinding, type VNode } from 'vue';
import StoreManager from './storage';

export default {
  mounted(el: HTMLElement, binding: DirectiveBinding, vnode: VNode) {
    handlePermission(el, binding, vnode);
  }
};


function handlePermission(el: HTMLElement, binding: DirectiveBinding, _vnode: VNode) {
  const { value } = binding;
  const permissions = StoreManager.get<MenuVO[]>("menuAuth"); // localStorage.getItem("menuAuth")
  //const permissionList = (permissions ? JSON.parse(permissions) : []) as MenuVO[];


  if (!permissions || permissions.length === 0 || !hasPermission(permissions, value)) {
    el.remove();
  }

}

function hasPermission(permissions: MenuVO[], menuCode: string): boolean {
  return permissions.some((permission: MenuVO) => permission.menuCode === menuCode);
}
